package com.abigdreamer.im.server.ui.desktop.internal;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import com.abigdreamer.im.server.ui.desktop.internal.MyVMWindow.IPAddress;


public class MyVMFrame extends DeskTopFrame{
	private static MyVMFrame instance;
	
	private MyVMWindow myVMWindow;
	
	public static MyVMFrame getInstance() {
		if(instance==null){
			instance=new MyVMFrame();
		}
		return instance;
	}
	
	private MyVMFrame(){
		super("服务器虚拟机信息", true);
		initComponents();
		this.setClosable(true);
		this.setBounds(100, 40, 550, 400);
	}
	
	 private void initComponents() {
		     myVMWindow =new MyVMWindow();
		    this.getContentPane().add(myVMWindow,BorderLayout.NORTH);
	    }
}
class MyVMWindow extends JPanel{
	public MyVMWindow(){
		this.setLayout(new BorderLayout());
		intNorthPan();
		intSouthPan();
	}
	
	private void intNorthPan(){
		JPanel northPan=new JPanel();
		northPan.setBorder(BorderFactory.createTitledBorder("虚拟机信息"));
		JScrollPane vmScrollTable=new JScrollPane();
		vmScrollTable.setAutoscrolls(true);
		List<PropertyBean> propertyBeans=JavaVM.getAllPropetys();
		VMTableModel tableModel=new VMTableModel(propertyBeans);
		JTable vMTable=new JTable();
		vMTable.setModel(tableModel);
		vMTable.setPreferredScrollableViewportSize(new Dimension(500, 150));
		vmScrollTable.setViewportView(vMTable);
		northPan.add(vmScrollTable,BorderLayout.CENTER);
		this.add(northPan,BorderLayout.NORTH);
	}
	private void intSouthPan(){
		JPanel southPan=new JPanel();
		southPan.setBorder(BorderFactory.createTitledBorder("网卡信息"));
		JScrollPane cardScrollTable=new JScrollPane();
		cardScrollTable.setAutoscrolls(true);
		List<IPAddress> propertyBeans=this.getAddressCarInfo();
		CardTableModel cardModel=new CardTableModel(propertyBeans);
		JTable cardTable=new JTable();
		cardTable.setModel(cardModel);
		cardTable.setPreferredScrollableViewportSize(new Dimension(500, 80));
		cardScrollTable.setViewportView(cardTable);
		southPan.add(cardScrollTable,BorderLayout.CENTER);
		System.out.println("intSouthPan");
		this.add(southPan,BorderLayout.SOUTH);
	}
	
	private List<IPAddress> getAddressCarInfo(){
		 List<IPAddress> propertys=new ArrayList<IPAddress>();
			try {
				Enumeration list = NetworkInterface.getNetworkInterfaces();
				while(list.hasMoreElements()) {
					IPAddress ipAddress=new IPAddress();
					NetworkInterface iFace = (NetworkInterface) list.nextElement();
					ipAddress.setCard(iFace.getDisplayName());
					Enumeration addresses = iFace.getInetAddresses();
			        while(addresses.hasMoreElements()){
			        	InetAddress adr = (InetAddress) addresses.nextElement();
			        	ipAddress.setHostAddress(adr.getHostAddress());
			        	ipAddress.setHostName(adr.getHostName());
			        }
			        propertys.add(ipAddress);	
				}
			} catch (SocketException se) {
				System.out.println("Failed discovering network cards!");
				System.out.println("Error: " + se);
				se.printStackTrace();
			}
		 return propertys;
	}
	
	class IPAddress {
		
		private String card;
		private String hostAddress;
		private String hostName;
		
		public String getCard() {
			return card;
		}
		public void setCard(String card) {
			this.card = card;
		}
		public String getHostAddress() {
			return hostAddress;
		}
		public void setHostAddress(String hostAddress) {
			this.hostAddress = hostAddress;
		}
		public String getHostName() {
			return hostName;
		}
		public void setHostName(String hostName) {
			this.hostName = hostName;
		}
		
}	
	
}

class VMTableModel implements TableModel{

	private List<PropertyBean> propertyBeans=new ArrayList<PropertyBean>() ;
	public VMTableModel(){
		
	}
	public VMTableModel(List<PropertyBean> propertyBeans){
		this.propertyBeans=propertyBeans;
	}
	
	public void addTableModelListener(TableModelListener l) {
		
	}

	public Class<?> getColumnClass(int columnIndex) {
		return String.class;
	}

	public int getColumnCount() {
		 return 2;
	}

	public String getColumnName(int columnIndex) {
		 if(columnIndex==0){//第一列是UserInfo对象的id值
	        	return "属性名";
	        }else if(columnIndex==1){//第二列是name属性值
	        	return "属性值";
	        }else{//除非设计时逻辑错误，否则不会到这里
	        	return "出错!";
	        }

	}

	public int getRowCount() {
		 return propertyBeans.size();
	}

	//得到指定单元格的值:第几行，就是列表中的第几个PropertyBean对象
	public Object getValueAt(int rowIndex, int columnIndex) {
	PropertyBean bean=propertyBeans.get(rowIndex);
     if(columnIndex==0){
     	return bean.getName();
     }else if(columnIndex==1){
     	return bean.getValue();
     }else{
     	return "error";
     }

	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if(columnIndex!=0){
			 return true;
			 }
			 return false;
	}

	public void removeTableModelListener(TableModelListener l) {
		
	}

	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		
	}
	
}

class CardTableModel extends VMTableModel{
	List<IPAddress> propertys=new ArrayList<IPAddress>();
	public CardTableModel(List<IPAddress> propertys){
		this.propertys=propertys;
	}
	public Class<?> getColumnClass(int columnIndex) {
		return String.class;
	}

	public int getColumnCount() {
		 return 3;
	}

	public String getColumnName(int columnIndex) {
		 if(columnIndex==0){//第一列是UserInfo对象的id值
	        	return "网卡接口名";
	        }else if(columnIndex==1){//第二列是name属性值
	        	return "主 机 名";
	        }else if(columnIndex==2){//第二列是name属性值
	        	return "主 机 IP";
	        }else{//除非设计时逻辑错误，否则不会到这里
	        	return "出错!";
	        }

	}

	public int getRowCount() {
		 return propertys.size();
	}

	//得到指定单元格的值:第几行，就是列表中的第几个PropertyBean对象
	public Object getValueAt(int rowIndex, int columnIndex) {
		IPAddress bean=propertys.get(rowIndex);
     if(columnIndex==0){
     	return bean.getCard();
     }else if(columnIndex==1){
     	return bean.getHostName();
     }else if(columnIndex==2){
     	return bean.getHostAddress();
     }else{
     	return "error";
     }

	}
}
